//
//  DropMenu-Button.swift
//  soapp
//
//  Created by zhu xietong on 2017/10/13.
//

import UIKit
import Eelay

public class DropItem {
    
    static var selected_color:UIColor = .black
    static var normal_color:UIColor = .gray
    
    public var width:CGFloat? = nil
    public var selected_icon:UIImage?
    public var normal_icon:UIImage?
    public var normal_title:String?
    public var selected_title:String?
    
    public var drop_view:DropContentView?
    {
        didSet{
            drop_view?.item = self
        }
    }
    
    public var normal_title_color:UIColor = DropItem.normal_color
    public var selected_title_color:UIColor = DropItem.selected_color

    public var isSelected = false
    {
        didSet{
            self.cell?.isSelected = isSelected
        }
    }
    
    weak var cell:UICollectionViewCell? = nil
    
    public init(title:String?=nil,icon:UIImage?=nil) {
        self.normal_title = title
        self.normal_icon = icon
        self.selected_title = title
        self.selected_icon = icon
    }
}


open class DropItemCell:UICollectionViewCell
{
    let titleL = UILabel()
    let iconV = UIImageView()
    public override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.eelay = [
            [titleL,[ee.X.Y,[-12,0]]],
            [iconV,[ee.Y],"16",16,[titleL,ee.R,ee.L,4]],
        ]
        titleL.textAlignment = .center
        titleL.font = UIFont.systemFont(ofSize: 15)
        contentView.clipsToBounds = true
    }
    
    var item:DropItem = DropItem()
    {
        didSet{
            item.cell = self
            self.updateUI()
        }
    }
    
    func updateUI() {
        if item.isSelected
        {
            iconV.image = item.selected_icon
            if let _ = item.selected_icon
            {
                titleL.text = item.selected_title ?? ""
            }
            else{
                titleL.text = ".                                   \(item.selected_title ?? "")                             ."
            }
            titleL.textColor = item.selected_title_color
        }
        else{
            iconV.image = item.normal_icon
            if let _ = item.normal_icon
            {
                titleL.text = item.normal_title ?? ""
            }
            else{
                titleL.text = ".                                   \(item.normal_title ?? "")                             ."
            }
            titleL.textColor = item.normal_title_color
        }
    }
    
    open override var isSelected: Bool{
        set{
//            self.item.isSelected = newValue
            self.updateUI()
        }
        get{
            return self.item.isSelected
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

